#include "vd_fb.h"

static struct platform_device *vd_fb_dev = NULL;

/*
* module para
*/
static vd_csocket vd_fb_csock;

static char *server_ip = "192.168.0.199";
//module_param(server_ip, charp, S_IRUGO);

static int server_port = 20001;
module_param(server_port, int, S_IRUGO);

static int fb_buff_size = 32;
module_param(fb_buff_size, int, S_IRUGO);

static struct platform_driver vd_fb_driver = {
    .probe = vd_fb_drv_probe,
    .remove = vd_fb_drv_remove,
    .driver = {
        .name = DEVICE_NAME,
    },
};

static int __init vd_fb_init(void)
{
    int err = 0;

	printk("Initialize virtual framebuffer\n");

	vd_fb_dev = platform_device_register_simple(DEVICE_NAME, -1, NULL, 0);

	if (!vd_fb_dev) {
        printk("vd fb dev init fails\n");
        err = -1;

        goto error;
	}

	vd_fb_csock.buff_size = fb_buff_size;
	vd_fb_csock.buff = kmalloc(vd_fb_csock.buff_size, GFP_KERNEL);

	if (vd_fb_csock.buff == NULL) {
        printk("vd fb sock buff malloc fails\n");
        err = -1;
        goto error;
	}

	err = vd_start_client_udp_socket(&vd_fb_csock, server_ip, server_port);

	if (err < 0) {
        printk("vd fb start sock fails\n");
        goto free_sock_buff;
	}

	err = platform_driver_register(&vd_fb_driver);

	if (err)
        goto free_sock_buff;

    return 0;

free_sock_buff:
    kfree(vd_fb_csock.buff);
    vd_fb_csock.buff = NULL;

error:
    return err;
}

static void __exit vd_fb_exit(void)
{
	vd_close_client_udp_socket(&vd_fb_csock);

	printk("vd close framebuffer socket\n");

	kfree(vd_fb_csock.buff);

	platform_driver_unregister(&vd_fb_driver);

	platform_device_unregister(vd_fb_dev);


	printk("Unload Virtual framebuffer\n");
}

module_init(vd_fb_init);
module_exit(vd_fb_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR( "lzhang" );
